package com.ch.common.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.ch.common.base.viewModel.BaseLibViewModel

abstract class BaseLibFragment<VM : BaseLibViewModel, DB : ViewDataBinding> : Fragment() {
    protected lateinit var mContext: FragmentActivity
    protected var mRootView: View? = null
    protected lateinit var mBinding: DB
    protected abstract val viewModel: VM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = requireActivity()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (null == mRootView) {
            mBinding = DataBindingUtil.inflate(inflater, getLayoutResID(), container, false)
            mBinding.lifecycleOwner = viewLifecycleOwner
            mRootView = mBinding.root
            initView()
            initObserve()
        } else {
            (mRootView?.parent as ViewGroup)?.removeView(mRootView)
        }
        return mRootView
    }

    abstract fun getLayoutResID(): Int
    abstract fun initView()
    abstract fun initObserve()
}